const ajax = require('../../tool/ajax')

/**
 * 根据仲裁员id搜索独任仲裁员 选择仲裁员
 * @param pupConfig 框架基础配置信息
 * @param Data 配置文件中的仲裁员信息list
 * @param type 类型 0:当事人选仲裁员页面,主任指定独任没有交集页面  1:主任指定独任有多个交集
 * 
 */
const start = async (pupConfig, Data, type = 0) => {
    page = pupConfig.page;
    if (type === 0) {
        let arr = await init(page, Data)
        //指定页面条数 40条/页
        await page.click('li.ant-pagination-options .ant-select-selection-selected-value')
        await page.click('ul.ant-select-dropdown-menu-vertical li.ant-select-dropdown-menu-item:nth-child(4)')
        await searchForArbitrators(page, arr, type)
    } else if (type === 1) {
        let index = await getItem(page, Data)
        let btnSelect = await page.$$('button.selectingAibitratorWidget-query-add span')
        await btnSelect[index].click()
    }
}
/**
 * 找出对应的仲裁员下标存入数组
 */
async function init(page, Data) {
    let res = await ajax.waitForResponse(page, 'arbitratorRecommend.do');
    let arr = []
    res = res.data.arbitratorList
    for (let i = 0; i < res.length; i++) {
        for (let j = 0; j < Data.length; j++) {
            if (Data[j].id === res[i].arbitratorId) {
                arr.push(i)
            }
        }
    }
    return arr
}
/**
 * 根据下标选择仲裁员
 */
async function searchForArbitrators(page, arr) {
    for (let k = 0; k < arr.length; k++) {
        let frequency = parseInt(arr[k] / 40) //页数 
        let num = arr[k] % 40//该页的下标
        //选择页面
        for (let l = 0; l < frequency; l++) {
            await page.click('.ant-pagination-next')
        }
        let btnSelect = await page.$$('button.selectingAibitratorWidget-1-query-add span')
        await btnSelect[num].click()
        await page.click('.ant-pagination-item-1')//回到第一页
    }
}
/**
 * 获取目标仲裁员的index
 */
async function getItem(page, Data) {
    let res = await ajax.waitForResponse(page, 'getSelectedArbitrator.do');
    res = res.data.arbitratorList
    for (let i = 0; i < res.length; i++) {
        if (Data[0].id === res[i].arbitratorId) {
            return i
        }
    }

}
module.exports = start;
